package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-01-07 01:00
 */
public class Q92 {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;

        ListNode pre = dummy;
        ListNode next = null;

        int index = 1;
        while (index < m) {
            pre = head;
            head = head.next;
            index++;
        }

        ListNode node = pre;

        while (index >= m && index <= n) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = head.next;
            index++;
        }

        node.next.next = head;
        node.next = pre;

        return dummy.next;
    }
}
